package demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息生产者
 */
public class Send {
    public static void main(String[] args) throws IOException, TimeoutException {
            //3.1 消息直接发给队列
            //3.2 消息发给交换机
        //1.建立连接
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();
        //2.建立通道
        Channel channel = connection.createChannel();
        //3.发送消息
        //创建队列
        channel.queueDeclare("simple_queue",false,false,false,null);

        String message = "Hello World33333!";
        channel.basicPublish("","simple_queue",null,message.getBytes());
        System.out.println("Send发送信息："+message);

        //4.关闭管道和连接
        channel.close();
        connection.close();
    }
}
